#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *createlist();
struct ListNode *reverse( struct ListNode *head );

void printlist( struct ListNode *head )
{
     struct ListNode *p = head;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    struct ListNode  *head;

    head = createlist();
    head = reverse(head);
    printlist(head);
    
    return 0;
}

struct ListNode *createlist()
{
	struct ListNode *head, *second, *third;
	head = (struct ListNode *)malloc(sizeof(struct ListNode));
	head->data = 1;
	
	second = (struct ListNode *)malloc(sizeof(struct ListNode));
	second->data = 2;
	head->next = second;
	
	third = (struct ListNode *)malloc(sizeof(struct ListNode));
	third->data = 3;
	second->next = third;
	third->next = NULL;
	
	return head;
} 

struct ListNode *reverse( struct ListNode *head )
{
	struct ListNode *prev, *current, *nextNode;
	prev = NULL;
	current = head;
	
	while(current != NULL)
	{
		nextNode = current->next;
		current->next = prev; 
		prev = current;
		current = nextNode;
	}
	
	head = prev;
	
	return head;
}
